<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>1.数组测试平台</title>
</head>
<body>
<script>
    function CArray(elms){
        this.data=[];
        this.pos=0;
        this.elms=elms;
        for(var i=0;i<elms;i++){
            this.data[i]=i;
        }
    }
    CArray.prototype={
        insert: function (elm) {
            this.data[this.pos++]=elm;
        },
        toString: function () {
            console.log('data:',this.data);
        },
        clear: function () {
            for(var i=0;i<this.data.length;i++){
                this.data[i]=0;
            }
        },
        setData:function(){
            for(var i= 0;i<this.elms;i++){
                this.data[i]=Math.floor(Math.random()*(this.elms+1));
            }
        },
        swap: function (arr,i1,i2) {
            var t=arr[i1];
            arr[i1]=arr[i2];
            arr[i2]=t;
        },
        bubble: function () {
            var length=this.data.length;
            for(var outer=length;outer>=2;--outer){
                console.log(outer);
                for(var inner=0;inner<=outer;++inner){
                    if(this.data[inner]>this.data[inner+1]){
                        this.swap(this.data,inner,inner+1);
                    }
                }
            }
        }
    };


    var ca=new CArray(10);
    ca.setData();
    ca.bubble();
    ca.toString();
    

</script>
</body>
</html>